package com.ecb.business.service.impl;

import com.ecb.business.constant.AuditConstant;
import com.ecb.business.dto.contract.manage.ContractDTO;
import com.ecb.business.dto.contract.manage.SearchContractDTO;
import com.ecb.business.exception.BusinessException;
import com.ecb.business.po.ChainContractPO;
import com.ecb.business.repository.ChainContractDao;
import com.ecb.business.service.AuditService;
import com.ecb.business.service.ContractManageService;
import com.ecb.business.service.UserManageService;
import com.ecb.business.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import static com.ecb.business.constant.AuditConstant.OPERATE_TARGET_CONTRACT;
import static com.ecb.business.constant.AuditConstant.OPERATE_TYPE_ADD_CONTRACT;

@Service
public class ContractManageServiceImpl implements ContractManageService {

    @Autowired
    ChainContractDao chainContractDao;
    @Autowired
    AuditService auditService;
    @Autowired
    UserManageService userManageService;

    @Override
    public List<ChainContractPO> getAllContractByChainId(SearchContractDTO searchContractDTO) {
        return chainContractDao.findAllByContractNameLikeAndStatusEqualsAndChainIdEquals(searchContractDTO.getContractName(), 1, searchContractDTO.getChainId());
    }

    @Override
    public boolean addContract(ContractDTO contractDTO) {
        userManageService.checkIsAdminElseThrow();
        ChainContractPO chainContractPO = JsonUtils.beanToBean(contractDTO, ChainContractPO.class);
        chainContractDao.save(chainContractPO);
        auditService.addAudit(OPERATE_TYPE_ADD_CONTRACT, OPERATE_TARGET_CONTRACT, "admin", null, JsonUtils.beanToJson(chainContractPO));
        return true;
    }

    @Override
    public boolean updateContract(ContractDTO contractDTO) {
        userManageService.checkIsAdminElseThrow();
        ChainContractPO chainContractPO = JsonUtils.beanToBean(contractDTO, ChainContractPO.class);
        Optional<ChainContractPO> byId = chainContractDao.findById(chainContractPO.getId());
        if (!byId.isPresent()){
            throw new BusinessException("500", "合约不存在");
        }
        chainContractPO.setModifyTime(new Date());
        chainContractDao.save(chainContractPO);
        auditService.addAudit(AuditConstant.OPERATE_TYPE_UPDATE_CONTRACT, AuditConstant.OPERATE_TARGET_CONTRACT, chainContractPO.getModifyUser(), JsonUtils.beanToJson(byId.get()), JsonUtils.beanToJson(chainContractPO));
        return true;
    }

    @Override
    public boolean deleteContract(ContractDTO contractDTO) {
        userManageService.checkIsAdminElseThrow();
        ChainContractPO chainContractPO = JsonUtils.beanToBean(contractDTO, ChainContractPO.class);
        Optional<ChainContractPO> byId = chainContractDao.findById(chainContractPO.getId());
        if (!byId.isPresent()){
            throw new BusinessException("500", "合约不存在");
        }
        chainContractPO.setStatus(9);
        chainContractPO.setModifyTime(new Date());
        chainContractDao.save(chainContractPO);
        auditService.addAudit(AuditConstant.OPERATE_TYPE_DELETE_CONTRACT, AuditConstant.OPERATE_TARGET_CONTRACT, chainContractPO.getModifyUser(), JsonUtils.beanToJson(byId.get()), JsonUtils.beanToJson(chainContractPO));
        return true;
    }

}
